package com.springboot.demo.web;

//import com.springboot.demo.entity.BRJson;
import com.springboot.demo.entity.Book;
import com.springboot.demo.entity.Reader;
import com.springboot.demo.entity.ReaderJPA;
import com.springboot.demo.service.BaseSetupService;
import com.springboot.demo.service.ReaderService;
import com.springboot.demo.service.LibrarianViewReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LibrarianViewReaderController {
    @Autowired
    LibrarianViewReaderService librarianViewReaderService;
    @Autowired
    private ReaderService readerService;

    /**
     * 进入界面
     */
    @RequestMapping("/librarian_BR_viewReader")
    public String librarian_book() {
        return "librarian_BR_viewReader";
    }

    // 查看读者信息
    @ResponseBody
    @PostMapping("/librarian_view_reader")
    public List<ReaderJPA> findAllReaders() {
        List<ReaderJPA> readerList = librarianViewReaderService.findAllReaders();
        return readerList;
    }

    // 查看借书信息
    @ResponseBody
    @PostMapping("/librarian_view_br")
    public List<Map> findAllBRs(@RequestParam("phone") String phone) {
        String sql = "select bk.bname,bk.author,bk.id,re.state,re.start_time,re.end_time,re.fine\n" +
                "from library.book bk,(SELECT b.ID,b.start_time,r.end_time,b.state,r.fine\n" +
                "FROM library.borrow b left join library.returntable r \n" +
                "on b.phone = r.phone and b.id = r.id\n" +
                "where b.phone = '" + phone + "') re\n" +
                "where bk.id = re.id;";

        List<Map> mapList = librarianViewReaderService.mySelect(sql);
        return mapList;
    }

}
